package com.sgb.kjwl.view.ui.activity.pick;


import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPickTenderDetailBinding;
import com.sgb.kjwl.model.entity.pick.TenderDictEntity3;
import com.sgb.kjwl.view.ui.adapter.pick.PickNoticeDetailAdapter;
import com.sgb.kjwl.viewmodel.pick.PickTenderInquiryModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * 招标详情-清单列表
 */
public class PickTenderDetailListActivity extends AppActivity {

    private PickNoticeDetailAdapter adapter;
    private ActivityPickTenderDetailBinding binding;
    private PickTenderInquiryModel sModel;
    private String tenderId;
    private boolean isMore = false;
    private int page = 1;

    public static void start(Context context, String tenderId) {
        Intent intent = new Intent(context, PickTenderDetailListActivity.class);
        intent.putExtra("tenderId", tenderId);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        tenderId = getIntent().getStringExtra("tenderId");
        binding = DataBindingUtil.setContentView(this, R.layout.activity_pick_tender_detail);
        binding.setLifecycleOwner(this);
        sModel = new ViewModelProvider(this).get(PickTenderInquiryModel.class);
        sModel.findDetailGoodsPage(tenderId, page);
        registerObserver();
        initData();
    }

    private void registerObserver() {
        sModel.dialog.observe(this, aBoolean -> {
            if (aBoolean) {
                if (!DialogHelper.isShowing()) {
                    DialogHelper.showProgressDialog(this, null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                }
            } else {
                DialogHelper.dismissProgressDialog();
            }
        });
        sModel.tenderDetailList.observe(this, new Observer<List<List<TenderDictEntity3>>>() {
            @Override
            public void onChanged(List<List<TenderDictEntity3>> lists) {
                binding.recyclerView.refreshComplete();
                binding.recyclerView.setPullRefreshEnabled(false);
                if (isMore) {
                    binding.recyclerView.loadMoreComplete();
                    if (lists.size() > 0) {
                        adapter.addDatas(lists);
                    }
                } else {
                    if (lists.size() > 0) {
                        adapter.setDatas(lists);
                    } else {
                        adapter.setDatas(null);
                    }
                }
            }
        });
        sModel.showToast.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MToast.showToast(getActivity(), s);
            }
        });
    }

    private void initData() {
        binding.tvTitle.setText("招标清单");
        if (adapter == null) {
            adapter = new PickNoticeDetailAdapter(this, null);
        }

        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_list_divide1));
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isMore = false;
                page = 1;
                sModel.findDetailGoodsPage(tenderId, page);
            }

            @Override
            public void onLoadMore() {
                isMore = true;
                page = page + 1;
                sModel.findDetailGoodsPage(tenderId, page);
            }
        });

        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.imgBackTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.recyclerView.getLayoutManager().scrollToPosition(0);
            }
        });
    }
}